{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center><font size=5>姓名：冉成林   学号：20201120466   专业：电子科学与技术</font></center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center><font size=5>邮箱：2033947824@qq.com</font></center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center><font size=6>Project #3</font></center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center><font size=5>利用Newton法求解非线性电路的工作点</font></center>"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div align=center> <img src=\"https://s1.328888.xyz/2022/04/28/AXISq.png![image.png](attachment:image.png)\" width=\"900\"></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center><font size=4>日期：2022.4.12</font></center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Description of the problem（项目介绍）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We have learned how to solve nonlinear systems of equations with newton method. In this project we are going to apply the skills into the analysis of nonlinear electronic circuits, which consists of resistors and a diode (a nonlinear element)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### The circuit to be solved（带求解的电路图）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div align=center> <img src=\"https://s1.328888.xyz/2022/04/28/AXz5g.png\" width=\"500\"></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step #1 Solve the circuit（求解电路）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use what you have learn during your circuit analysis（电路原理）course to write down the KCL or KVL equations.\n",
    "\n",
    "<!--- Use markdown to derive the process of the circuit analysis process, that is either use KCL or KVL to convert the circuit into systems of equations --->\n",
    "\n",
    "#### 电路具体求解过程\n",
    "\n",
    "$$\n",
    "(1/{R_1}+1/{R_2}+1/{R_6})×{v_1}-1/{R_2}×{v_2}-1/{R_6}×{v_3}=-5/{R_6}\\\\\n",
    "1/{R_2}×{v_1}-(1/{R_2}+1/{R_4})×{v_2}+1/{R_4}×{v_3}=5.95×10^{-6}(e^{{v_2}/0.026}-1)\\\\\n",
    "-1/{R_6}×{v_1}-1/{R_4}×{v_2}+(1/{R_4}+1/{R_5}+1/{R_6})×{v_3}=5/{R_6}\n",
    "$$\n",
    "\n",
    "\n",
    "#### 最终需要求解的方程组\n",
    "$$\n",
    "(1/500+1/11000){v_1}-1/11000{v_2}-1/1000{v_3}=-1/200\\\\\n",
    "1/11000{v_1}-(1/1000+1/11000){v_2}+1/1000{v_3}=5.95×10^{-6}(e^{{v_2}/0.026}-1)\\\\\n",
    "-1/1000{v_1}-1/1000{v_2}+(1/500+1/11000){v_3}=1/200\n",
    "$$\n",
    "#### 写出对应的函数\n",
    "$$\n",
    "f_1(v_1,v_2,v_3)=(1/500+1/11000){v_1}-1/11000{v_2}-1/1000{v_3}+1/200\\\\\n",
    "f_2(v_1,v_2,v_3)=1/11000{v_1}-(1/1000+1/11000){v_2}+1/1000{v_3}-5.95×10^{-6}(e^{{v_2}/0.026}-1)\\\\\n",
    "f_3(v_1,v_2,v_3)=-1/1000{v_1}-1/1000{v_2}+(1/500+1/11000){v_3}-1/200\n",
    "$$\n",
    "#### 写出F(X)\n",
    "$$\n",
    "F(V)=\\begin{bmatrix}\n",
    "(1/500+1/11000){v_1}-1/11000{v_2}-1/1000{v_3}+1/200\\\\\n",
    "1/11000{v_1}-(1/1000+1/11000){v_2}+1/1000{v_3}-1.113717×10^{-5}(e^{{v_2}/0.026}-1)\\\\\n",
    "-1/1000{v_1}-1/1000{v_2}+(1/500+1/11000){v_3}-1/200\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "#### 写出DF(X)\n",
    "$$\n",
    "DF(V)=\\begin{bmatrix}\n",
    "1/500+1/11000&-1/11000&-1/1000\\\\\n",
    "1/11000&-1/1000-1/11000-4.30004699246×10^{-4} e^{{v_2}/0.026}&1/1000\\\\\n",
    "-1/1000&-1/1000&1/500+1/11000\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "\n",
    "<!--- Revise markdown above according to the circuit KCL or KVL equations --->"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step #2 Python code for F(X)（写出函数向量的计算函数）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define the F(X) function below\n",
    "def F(V):\n",
    "    f1=(1/500+1/11000)*V[0]-1/11000*V[1]-1/1000*V[2]+1/200\n",
    "    f2=1/11000*V[0]-(1/1000+1/11000)*V[1]+1/1000*V[2]-1.113717*10**(-5)*(2.718281828459**(V[1]/0.0259)-1)\n",
    "    f3=-1/1000*V[0]-1/1000*V[1]+(1/500+1/11000)*V[2]-1/200\n",
    "    return np.array([f1,f2,f3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3.90909091e-03, -3.82849671e+28, -1.72727273e-03])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# import numpy library below\n",
    "import numpy as np\n",
    "# Test F(X) here\n",
    "Vold=np.array([1,2,3])\n",
    "F(Vold)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step #3 Python code for DF(X)（写出偏微分矩阵的计算函数）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define the DF(X) function below\n",
    "def DF(V):\n",
    "    df11=1/500+1/11000\n",
    "    df12=-1/11000\n",
    "    df13=-1/1000\n",
    "    df21=1/11000\n",
    "    df22=-1/1000-1/11000-4.30004699246*10**(-4)*(2.718281828459)**(V[1]/0.0259)\n",
    "    df23=1/1000\n",
    "    df31=-1/1000\n",
    "    df32=-1/1000\n",
    "    df33=1/500+1/11000\n",
    "    return np.array([[df11,df12,df13],\n",
    "                    [df21,df22,df23],\n",
    "                    [df31,df32,df33]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2.09090909e-03, -9.09090909e-05, -1.00000000e-03],\n",
       "       [ 9.09090909e-05, -2.52115714e+13,  1.00000000e-03],\n",
       "       [-1.00000000e-03, -1.00000000e-03,  2.09090909e-03]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Test DF(X) here\n",
    "Vold=np.array([1,1,1])\n",
    "DF(Vold)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step #4 Python code for the newton iteration function（写出newton迭代函数）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define calXnew(Xold) function below\n",
    "def calVnew(V):\n",
    "    s=np.linalg.inv(DF(V))@F(V)\n",
    "    Vnew=V-s\n",
    "    return Vnew"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-1.2738471   0.97409989  2.24794699]\n"
     ]
    }
   ],
   "source": [
    "# Test calXnew(Xold) here\n",
    "Vnew=calVnew(Vold)\n",
    "print(Vnew)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step #5 Python code for the iteration process（进行迭代计算）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.8686946058716067\n",
      "第 1 次迭代 \tVnew [-1.00105166  1.7470203   2.74807196]\n",
      "0.00034507941689748163\n",
      "第 2 次迭代 \tVnew [-1.01019288  1.72112018  2.73131306]\n",
      "0.00034507941689746363\n",
      "第 3 次迭代 \tVnew [-1.01933409  1.69522007  2.71455416]\n",
      "0.00034507941689748385\n",
      "第 4 次迭代 \tVnew [-1.02847531  1.66931996  2.69779527]\n",
      "0.00034507941689747393\n",
      "第 5 次迭代 \tVnew [-1.03761652  1.64341985  2.68103637]\n",
      "0.0003450794168974699\n",
      "第 6 次迭代 \tVnew [-1.04675774  1.61751973  2.66427748]\n",
      "0.00034507941689748163\n",
      "第 7 次迭代 \tVnew [-1.05589896  1.59161962  2.64751858]\n",
      "0.00034507941689746363\n",
      "第 8 次迭代 \tVnew [-1.06504017  1.56571951  2.63075968]\n",
      "0.000345079416897488\n",
      "第 9 次迭代 \tVnew [-1.07418139  1.5398194   2.61400079]\n",
      "0.0003450794168974699\n",
      "第 10 次迭代 \tVnew [-1.08332261  1.51391929  2.59724189]\n",
      "0.00034507941689747757\n",
      "第 11 次迭代 \tVnew [-1.09246382  1.48801917  2.58048299]\n",
      "0.00034507941689748163\n",
      "第 12 次迭代 \tVnew [-1.10160504  1.46211906  2.5637241 ]\n",
      "0.00034507941689746363\n",
      "第 13 次迭代 \tVnew [-1.11074625  1.43621895  2.5469652 ]\n",
      "0.00034507941689747757\n",
      "第 14 次迭代 \tVnew [-1.11988747  1.41031884  2.53020631]\n",
      "0.00034507941689747393\n",
      "第 15 次迭代 \tVnew [-1.12902869  1.38441872  2.51344741]\n",
      "0.00034507941689747627\n",
      "第 16 次迭代 \tVnew [-1.1381699   1.35851861  2.49668851]\n",
      "0.00034507941689748163\n",
      "第 17 次迭代 \tVnew [-1.14731112  1.3326185   2.47992962]\n",
      "0.00034507941689746363\n",
      "第 18 次迭代 \tVnew [-1.15645233  1.30671839  2.46317072]\n",
      "0.00034507941689747757\n",
      "第 19 次迭代 \tVnew [-1.16559355  1.28081827  2.44641182]\n",
      "0.00034507941689748163\n",
      "第 20 次迭代 \tVnew [-1.17473477  1.25491816  2.42965293]\n",
      "0.0003450794168974699\n",
      "第 21 次迭代 \tVnew [-1.18387598  1.22901805  2.41289403]\n",
      "0.00034507941689747757\n",
      "第 22 次迭代 \tVnew [-1.1930172   1.20311794  2.39613514]\n",
      "0.00034507941689746634\n",
      "第 23 次迭代 \tVnew [-1.20215841  1.17721783  2.37937624]\n",
      "0.00034507941689748163\n",
      "第 24 次迭代 \tVnew [-1.21129963  1.15131771  2.36261734]\n",
      "0.00034507941689747627\n",
      "第 25 次迭代 \tVnew [-1.22044085  1.1254176   2.34585845]\n",
      "0.00034507941689747263\n",
      "第 26 次迭代 \tVnew [-1.22958206  1.09951749  2.32909955]\n",
      "0.00034507941689748385\n",
      "第 27 次迭代 \tVnew [-1.23872328  1.07361738  2.31234066]\n",
      "0.00034507941689747393\n",
      "第 28 次迭代 \tVnew [-1.2478645   1.04771726  2.29558176]\n",
      "0.00034507941689746363\n",
      "第 29 次迭代 \tVnew [-1.25700571  1.02181715  2.27882286]\n",
      "0.00034507941689748163\n",
      "第 30 次迭代 \tVnew [-1.26614693  0.99591704  2.26206397]\n",
      "0.00034507941689747627\n",
      "第 31 次迭代 \tVnew [-1.27528814  0.97001693  2.24530507]\n",
      "0.0003450794168974784\n",
      "第 32 次迭代 \tVnew [-1.28442936  0.94411681  2.22854617]\n",
      "0.00034507941689745155\n",
      "第 33 次迭代 \tVnew [-1.29357058  0.9182167   2.21178728]\n",
      "0.00034507941689745675\n",
      "第 34 次迭代 \tVnew [-1.30271179  0.89231659  2.19502838]\n",
      "0.0003450794168973954\n",
      "第 35 次迭代 \tVnew [-1.31185301  0.86641648  2.17826949]\n",
      "0.000345079416897264\n",
      "第 36 次迭代 \tVnew [-1.32099422  0.84051637  2.16151059]\n",
      "0.0003450794168969092\n",
      "第 37 次迭代 \tVnew [-1.33013544  0.81461625  2.14475169]\n",
      "0.00034507941689590634\n",
      "第 38 次迭代 \tVnew [-1.33927666  0.78871614  2.1279928 ]\n",
      "0.0003450794168931677\n",
      "第 39 次迭代 \tVnew [-1.34841787  0.76281603  2.1112339 ]\n",
      "0.0003450794168856526\n",
      "第 40 次迭代 \tVnew [-1.35755909  0.73691592  2.094475  ]\n",
      "0.0003450794168650775\n",
      "第 41 次迭代 \tVnew [-1.3667003   0.7110158   2.07771611]\n",
      "0.00034507941680859315\n",
      "第 42 次迭代 \tVnew [-1.37584152  0.68511569  2.06095721]\n",
      "0.00034507941665372653\n",
      "第 43 次迭代 \tVnew [-1.38498274  0.65921558  2.04419832]\n",
      "0.00034507941622903223\n",
      "第 44 次迭代 \tVnew [-1.39412395  0.63331547  2.02743942]\n",
      "0.0003450794150645913\n",
      "第 45 次迭代 \tVnew [-1.40326517  0.60741535  2.01068052]\n",
      "0.00034507941187202393\n",
      "第 46 次迭代 \tVnew [-1.41240638  0.58151524  1.99392163]\n",
      "0.00034507940311953995\n",
      "第 47 次迭代 \tVnew [-1.4215476   0.55561513  1.97716273]\n",
      "0.0003450793791262242\n",
      "第 48 次迭代 \tVnew [-1.43068882  0.52971502  1.96040384]\n",
      "0.00034507931335756085\n",
      "第 49 次迭代 \tVnew [-1.43983003  0.50381491  1.94364494]\n",
      "0.00034507913309010153\n",
      "第 50 次迭代 \tVnew [-1.44897124  0.47791481  1.92688605]\n",
      "0.00034507863902328244\n",
      "第 51 次迭代 \tVnew [-1.45811245  0.45201473  1.91012718]\n",
      "0.00034507728500608764\n",
      "第 52 次迭代 \tVnew [-1.46725364  0.42611469  1.89336833]\n",
      "0.000345073574511912\n",
      "第 53 次迭代 \tVnew [-1.47639478  0.4002148   1.87660958]\n",
      "0.0003450634072246457\n",
      "第 54 次迭代 \tVnew [-1.48553578  0.37431529  1.85985107]\n",
      "0.000345035550237432\n",
      "第 55 次迭代 \tVnew [-1.49467642  0.34841682  1.84309324]\n",
      "0.0003449592386285095\n",
      "第 56 次迭代 \tVnew [-1.50381604  0.32252122  1.82633726]\n",
      "0.0003447502627325092\n",
      "第 57 次迭代 \tVnew [-1.51295289  0.29663347  1.80958636]\n",
      "0.0003441784738093718\n",
      "第 58 次迭代 \tVnew [-1.52208217  0.27076719  1.79284936]\n",
      "0.00034261741397528144\n",
      "第 59 次迭代 \tVnew [-1.53119072  0.24495963  1.77615035]\n",
      "0.0003383806440465078\n",
      "第 60 次迭代 \tVnew [-1.54024277  0.21931214  1.75955492]\n",
      "0.00032706479612923655\n",
      "第 61 次迭代 \tVnew [-1.54914219  0.19409714  1.74323933]\n",
      "0.0002981239916254224\n",
      "第 62 次迭代 \tVnew [-1.55763874  0.17002356  1.72766231]\n",
      "0.0002321706455934313\n",
      "第 63 次迭代 \tVnew [-1.56513679  0.1487791   1.71391589]\n",
      "0.00012010525118261997\n",
      "第 64 次迭代 \tVnew [-1.57052973  0.13349911  1.70402884]\n",
      "2.3128134047245142e-05\n",
      "第 65 次迭代 \tVnew [-1.57289627  0.1267939   1.69969017]\n",
      "5.414877995348992e-07\n",
      "第 66 次迭代 \tVnew [-1.57325838  0.12576792  1.6990263 ]\n",
      "2.2056687136824274e-10\n",
      "第 67 次迭代 \tVnew [-1.57326569  0.12574722  1.69901291]\n",
      "3.401282520421234e-17\n",
      "第 68 次迭代 \tVnew [-1.57326569  0.12574721  1.6990129 ]\n"
     ]
    }
   ],
   "source": [
    "# Initialize Xold vector below\n",
    "Vold=np.array([0,0,0])\n",
    "for i in range(1,100):\n",
    "    Vnew=calVnew(Vold)\n",
    "    stdError=np.square(Vnew-Vold).mean()\n",
    "    print(stdError)\n",
    "    Vold=Vnew\n",
    "    print(\"第\",i,\"次迭代\",\"\\tVnew\",Vnew)\n",
    "    if stdError<0.000000000000001:\n",
    "        break\n",
    "# loop to do the iterations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step #6 Verify if the results are right（测试迭代结果）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.00000000e+00 1.87675396e-15 8.67361738e-19]\n"
     ]
    }
   ],
   "source": [
    "# Use the final Xnew vector as parameter for F(X), the result should be very close to [0, 0, 0]\n",
    "print(F(Vnew))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 用Pspice进行了仿真测试，由于模型不精，结果稍有误差"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div align=center> <img src=\"https://s1.328888.xyz/2022/04/28/AXCa1.png\" width=\"800\"></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step #7 My experience of Project #3（我做项目三的心得或者感受）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.用多元牛顿法求解非线性方程组的时候，需要由原方程得到F(X)和DF(X),其中DF(X)为函数导数对应的雅可比行列式。\n",
    "###   2.在进行迭代运算的时候，我感觉收敛速度是比较慢的。并且如果初始向量的某些元素一旦过大，会导致循环很多次，甚至会导致报错（可能已经超过了收敛域）。\n",
    "### 3.i=Is(e^(V/0.026)-1)这个模型在300.15K的温度下不完全精确，和仿真的值稍有不同。目前用了multisim和PSPICE，multisim的误差比PSPICE更大（Is不同）。我不敢肯定PSPICE和multisim的模型不同，但是我觉得这个项目中的模型比较简单，可能相比仿真软件的计算模型做了简化。后来我也用i=Is(e^(qu/kT)-1)的模型进行进一步精确，但结果差异不大。总之，这也将成为我的一个问题吧，看以后能不能通过一些书或者资料解决。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
